﻿using LaserAutoTest.Application.Models;
using LaserAutoTest.Application;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;

namespace LaserAutoTest.UI.Views
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindowView : Window
    {
        private UserControl _screen;
        public MainWindowView()
        {
            InitializeComponent();
            InitMainWindowSize();
            this.MaxHeight = SystemParameters.PrimaryScreenHeight;
            this.MaxWidth = SystemParameters.PrimaryScreenWidth;
        }
        private void WindowClose_Click(object sender, RoutedEventArgs e)
        {

            string message = "您确认要退出程序？";
            var result = GlobalValues.MessageBoxShow(message);

            if (!result)  // 取消退出操作
            {
                return;
            }
            else
            {
                GlobalValues.TcpServer.Stop();
                this.Close();
            }

        }

        private void Title_MouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            if (e.LeftButton == MouseButtonState.Pressed)
                this.DragMove();
        }
        private void WindowMiniSize_Click(object sender, RoutedEventArgs e)
        {
            this.WindowState = WindowState.Minimized;
        }

        private void WindowMaxSize_Click(object sender, RoutedEventArgs e)
        {
            if (this.WindowState == WindowState.Maximized)
                this.WindowState = WindowState.Normal;
            else
                this.WindowState = WindowState.Maximized;
            InitMainWindowSize();
            ResizeScreen();
        }
        public void SwitchScreen(object sender)
        {
            var screen = ((UserControl)sender);

            if (screen != null)
            {
                //StackPanelMain.Children.Clear();
                //StackPanelMain.Children.Add(screen);
                _screen = screen;
            }
        }

        private void ResizeScreen()
        {
            if (_screen != null)
            {
                _screen.Height = GlobalValues.MainWindowSize.Height - 100;
                _screen.Width = GlobalValues.MainWindowSize.Width - 250;
                SwitchScreen(_screen);
            }
        }
        private void InitMainWindowSize()
        {
            if (GlobalValues.MainWindowSize == null)
                GlobalValues.MainWindowSize = new MainWindowModel();
            GlobalValues.MainWindowSize.Height = this.Height;
            GlobalValues.MainWindowSize.Width = this.Width;
            ContentGrid.Width = this.Width;
            ContentGrid.UpdateLayout();
        }

        private void AnimationCompleted(object sender, System.EventArgs e)
        {
            ContentGrid.Width = this.Width;
            ContentGrid.UpdateLayout();
        }
    }
}
